package ru.spb.osll.myfit.ant;

import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

/**
 * Modify config.xml for deploy to Tomcat
 */
public class TomConfig extends Task {
    private  File file;
	private  File tofile;
	private  String appname;
    
    public void execute() throws BuildException {
     	String xmlHeader = "<?xml version='1.0' encoding='UTF-8'?>";
        try {
        	
        	DocumentBuilder xml = DocumentBuilderFactory.newInstance()
			.newDocumentBuilder();
	
        	Element root = xml.parse(file).getDocumentElement();
        	NodeList nl = root.getChildNodes();
        	for(int i = 0; i<nl.getLength(); i++)
        	{
        		Node node = nl.item(i);
        		if(node instanceof Element)
        		{
        			Element element = (Element) node;
        			String path = element.getAttribute("path");
        			if(path!=null)
        				element.setAttribute("path", "webapps/"+appname+"/WEB-INF/"+path);
        		}
        	}
        	
        	if(!tofile.exists())
        		tofile.createNewFile();
        	
        	FileOutputStream file = new FileOutputStream(tofile);
			file.write((xmlHeader+toString(root,null)).getBytes());
			file.close();
        	
        	
        	
           
        } catch (Exception e) {
            throw new BuildException(e);
        } 
    }

    public void setFile(File file) {
        this.file = file;
    }
    
    public void setTofile(File tofile) {
        this.tofile = tofile;
    }
    
    public void setAppname(String appname) {
        this.appname = appname;
    }
    
   
	public static String toString(Element item, String separator)
	{
		return toString(item, separator, false);
	}

	
	public static String toString(Element item, String separator, boolean indexed)
	{
		StringBuffer result = new StringBuffer();

		result.append("<").append(item.getTagName());
		for (int i = 0; i < item.getAttributes().getLength(); i++)
		{
			Node node = item.getAttributes().item(i);
			if (node != null)
				result.append(" ").append(node.getNodeName()).append("='").append(node.getNodeValue()).append("'");
		}

		if (!item.hasChildNodes())
		{
			result.append("/>").append((separator != null) ? separator : "");
			return result.toString();
		}
		result.append(">").append((separator != null) ? separator : "");

		NodeList nodeList = item.getChildNodes();
		int length = nodeList.getLength();
		for (int i = 0; i < length; i++)
		{
			Node node = nodeList.item(i);
			if (node instanceof Element)
				result.append(toString((Element) node, separator, indexed));
			else if (node.getNodeType() == Node.TEXT_NODE)
				result.append(node.getNodeValue());
		}
		result.append("</").append(item.getTagName()).append(">").append((separator != null) ? separator : "");

		return result.toString();
	}
	
    
}